﻿using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace ChuanYe.CountyFireManagement.BLL
{
    public class FireFrontCommandBLL
    {
        #region 构造单例
        private FireFrontCommandBLL() { }
        private static FireFrontCommandBLL _instance;
        public static FireFrontCommandBLL Instance
        {
            get
            {
                return _instance ?? (_instance = new FireFrontCommandBLL());
            }
        }
        #endregion

        public ResultDto<List<FireMQTTResponseModel>> Add(Firefrontcommand info)
        {
            ResultDto<List<FireMQTTResponseModel>> result = new ResultDto<List<FireMQTTResponseModel>>();
            var fireInfo = BLL.FireInfoBLL.Instance.Get(info.FireId);
            if (fireInfo == null)
            {
                result.Status = -1;
                result.Message = "FireInfo Is Not Exist";
            }
            //var lastRecordInfo = BLL.RecordBLL.Instance.GetLast(info.FireId, null);
            //if (lastRecordInfo == null || lastRecordInfo.FirstOrDefault().Type != (int)RecordType.火灾录入)
            //{
            //    return -2;
            //}
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                ef.Firefrontcommand.Add(info);
                ef.Entry<Firefrontcommand>(info).State = EntityState.Added;
                ef.SaveChanges();

                string longitudeString = string.Empty;
                string latitudeString = string.Empty;
                if (!string.IsNullOrEmpty(info.Latitude) && !string.IsNullOrEmpty(info.Longitude))
                {
                    //坐标地址字符串转换
                    string[] longitudeSplit = info.Longitude.Split('|');
                    longitudeString = longitudeSplit[0] + "度" + longitudeSplit[1] + "分" + longitudeSplit[2] + "秒";
                    string[] latitudeSplit = info.Latitude.Split('|');
                    latitudeString = latitudeSplit[0] + "度" + latitudeSplit[1] + "分" + latitudeSplit[2] + "秒";
                }

                string[] hmArray = info.HeadquarterMemberIds.Split(',');
                List<int> hmIDs = hmArray.Select(s => s.ToInt()).ToList();
                var hmList = ef.Fireheadquartermember.Where(p => hmIDs.Contains(p.HeadquarterMemberId)).ToList();
                string hmNames = string.Join(",", hmList.Select(s => s.Name).ToList());

                var userInfo = ef.Userinfo.Where(p => p.UserId == info.UserId).FirstOrDefault();
                Record record = new Record();
                record.FireId = fireInfo.FireId;
                record.HandleTime = info.TransDatetime;
                record.Type = (int)RecordType.成立前指;
                record.UserId = info.UserId;
                record.Content = string.Format(FireInfoTool.GetFireInfoRecordFormat(record.Type.Value),fireInfo.FireName,fireInfo.Address,longitudeString, latitudeString,
                                                info.CommanderName,hmNames);
                record.AddTime = info.TransDatetime;
                record.Ip = info.Ip;
                record.Applicant = userInfo.RealName;
                record.CustomData = info.FrontCommandId.ToString();
                ef.Record.Add(record);
                ef.Entry<Record>(record).State = EntityState.Added;

                Message message = new Message();
                message.FireId = fireInfo.FireId;
                message.Type = (int)MessageType.成立前指;
                message.Content = record.Content;
                message.SendType = (int)MessageSendType.上级向下级发送;
                message.IsRead = 0;
                message.HandleTime = info.TransDatetime;
                message.SendUserId = info.UserId;
                message.Ip = fireInfo.Ip;
                ef.Message.Add(message);
                ef.Entry<Message>(message).State = EntityState.Added;
                ef.SaveChanges();


                #region 装载消息
                List<Message> messages = new List<Message>() { message };
                result.FirstParam = MessageBLL.Instance.TransMessageToFireMQTTResponseModel(messages, record.Type.Value);
                #endregion
                
                return result;
            }
        }
    }
}
